﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Springwater.Business.Util
{
    public class GpsUtil
    {
        public static decimal? StringToDD(string gps)
        {
            if (!string.IsNullOrEmpty(gps))
            {
                string[] str = gps.Split(new char[] {'o'}, StringSplitOptions.RemoveEmptyEntries);
                double fok = double.Parse(str[0]);
                double perc = double.Parse(str[1]);

                return DmsToDD(fok, perc);
            }

            return null;
        }

        public static decimal DmsToDD(double d, double m = 0, double s = 0)
        {
            return Convert.ToDecimal((d + (m / 60) + (s / 3600)) * (d < 0 ? -1 : 1));
        }

        public static decimal ConvertDecimalToDegMinSec(decimal value, out int deg, out int min, out int sec)
        {
            deg = (int)value;
            value = Math.Abs(value - deg);
            min = (int)(value * 60);
            value = value - (decimal)min / 60;
            sec = (int)(value * 3600);
            value = value - (decimal)sec / 3600;
            return value;
        }

        public static string ConvertDecimalToString(decimal value)
        {
            int deg, min, sec = 0;

            GpsUtil.ConvertDecimalToDegMinSec(value, out deg, out min, out sec);
            value = min + (decimal)sec / (decimal)60;
            return string.Format("{0}o{1}", deg, value);
        }
    }
}
